using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour
{
    [Header("生成敌人的时间间隔 单位:秒")]
    public float TimeOfSummoningEnemy;
    [Header("战场边界 上右下左 这是人机能到达的最远的地方")]
    public float BattlefieldBorderTop = 4f;
    public float BattlefieldBorderRight = 3f;
    public float BattlefieldBorderBottom = -4f;
    public float BattlefieldBorderLeft = -8f;
    public GameObject[] enemyPrefab;
    public GameObject wormholePrefab;
    [Header("生成虫洞的时间间隔 单位:秒")]
    public float TimeOfSummoningWormhole = 10f;
    public GameManager gameManager;
    public int KillCount = 0;
    private int enemySummoned = 0;
    [Header("反转鸟生成概率")]
    public float reverseBirdRand = 0.3f;

    void Start()
    {
        int sceneIndex = gameManager.sceneIndex;
        Debug.Log("sceneIndex: " + sceneIndex);
        // 第一关
        InvokeRepeating("SummonEnemy", 0, TimeOfSummoningEnemy);
        // 从第三关开始新增虫洞
        if (sceneIndex == 3 || sceneIndex == 5)
        {
            InvokeRepeating("SummonWormhole", 0, TimeOfSummoningWormhole);
        }
    }

    void Update()
    {
        if (GameManager.Instance.IsGameOver)
        {
            CancelInvoke("SummonEnemy");
        }
    }

    public void SummonEnemy()
    {
        enemySummoned += 1;
        // 在距离玩家较远的两条边界中的一条生成敌人
        // 例如：玩家位置(-2, 2) 则在右边界外或下边界外生成敌人
        float playerX = GameObject.FindGameObjectWithTag("Player").transform.position.x;
        float playerY = GameObject.FindGameObjectWithTag("Player").transform.position.y;
        // 敌人生成时距离边界的长度
        float distanceToBorder = 3f;
        List<int> edges = new List<int>();
        if (playerX >= 0 && playerY >= 0)
        {
            // 下边界或左边界
            edges.Add(1);
            edges.Add(2);
        }
        else if (playerX < 0 && playerY >= 0)
        {
            // 下边界或右边界
            edges.Add(1);
            edges.Add(3);
        }
        else if (playerX < 0 && playerY < 0)
        {
            // 上边界或右边界
            edges.Add(0);
            edges.Add(3);
        }
        else
        {
            // 上边界或左边界
            edges.Add(0);
            edges.Add(2);
        }
        int edge = edges[Random.Range(0, 2)];
        float x = 0f, y = 0f;
        switch (edge)
        {
            // 注意 这里生成的地方是边界外而不是边界上
            case 0: // 上边界
                x = Random.Range(BattlefieldBorderLeft - distanceToBorder, BattlefieldBorderRight + distanceToBorder);
                y = BattlefieldBorderTop + distanceToBorder;
                break;
            case 1: // 下边界
                x = Random.Range(BattlefieldBorderLeft - distanceToBorder, BattlefieldBorderRight + distanceToBorder);
                y = BattlefieldBorderBottom - distanceToBorder;
                break;
            case 2: // 左边界
                x = BattlefieldBorderLeft - distanceToBorder;
                y = Random.Range(BattlefieldBorderBottom - distanceToBorder, BattlefieldBorderTop + distanceToBorder);
                break;
            case 3: // 右边界
                x = BattlefieldBorderRight + distanceToBorder;
                y = Random.Range(BattlefieldBorderBottom - distanceToBorder, BattlefieldBorderTop + distanceToBorder);
                break;
        }
        Vector3 spawnPosition = new Vector3(x, y, 0);
        int index = Random.Range(0, enemyPrefab.Length);
        GameObject newEnemy = Instantiate(enemyPrefab[index], spawnPosition, Quaternion.identity);
        newEnemy.GetComponent<SpriteRenderer>().sortingOrder = enemySummoned;
        newEnemy.GetComponent<Enemy>().IsReverseBird = Roll(reverseBirdRand);
    }

    // 生成虫洞
    public void SummonWormhole()
    {
        Instantiate(wormholePrefab);
    }

    // 更改分辨率
    private void ChangeResolution(int x, int y)
    {
        Screen.SetResolution(x, y, false);
    }

    public void IncreaseKillCount(int amount)
    {
        KillCount += amount;
    }

    // p 期望在 [0,1] 范围内
    bool Roll(float p)
    {
        p = Mathf.Clamp01(p);            // 防止传入非法值
        return Random.value <= p;        // Random.value 返回 [0.0, 1.0]
    }
}
